package com.app.micaihu.manager;

import android.app.Activity;
import android.content.SharedPreferences;

import com.app.micaihu.app.AppApplication;

/**
 * 程序配置管理者
 * （所有程序相关的配置全从这里获取）
 * Created by linkai on 2016/7/20.
 */
public class AppConfigManager {

    private static AppConfigManager appConfigManager;
    private static SharedPreferences configSP;//配置文件
    private String congigFileName = "config_file";

    private AppConfigManager(){}

    public static AppConfigManager getInstance(){
        if(appConfigManager == null){
         appConfigManager = new AppConfigManager();
        }
        return appConfigManager;
    }

    /**
     * 得到默认的配置文件
     * @return SharedPreferences
     */
    public SharedPreferences getConfigSP(){
        if(configSP == null){
            configSP = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        return configSP;
    }

    /**
     * 写入配置
     * @param key
     * @param value
     */
    public void writeConfig(String key,String value){
        if(configSP == null){
            configSP = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        if(configSP == null){
            return;
        }
        SharedPreferences.Editor editor = configSP.edit();
        editor.putString(key,value);
        editor.apply();
    }


    /**
     * 写入布尔值
     * @param key
     * @param flag
     */
    public void writeConfig(String key,boolean flag){
        if(configSP == null){
            configSP = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        if(configSP == null){
            return;
        }
        SharedPreferences.Editor editor = configSP.edit();
        editor.putBoolean(key,flag);
        editor.apply();
    }

    /**
     * 写入整型值
     * @param key
     * @param value
     */
    public void writeConfig(String key,int value){
        if(configSP == null){
            configSP = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        if(configSP == null){
            return;
        }
        SharedPreferences.Editor editor = configSP.edit();
        editor.putInt(key, value);
        editor.apply();
    }



    /**
     * 读取配置
     * @param key
     * @param defaultValue
     * @return String
     */
    public String readConfig(String key,String defaultValue){
        if(configSP == null){
            configSP = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        if(configSP == null){
            return defaultValue;
        }
        return configSP.getString(key,defaultValue);
    }

    /**
     * 读取配置
     * @param key
     * @param defaultValue
     * @return boolean
     */
    public boolean readConfig(String key,boolean defaultValue){
        if(configSP == null){
            configSP = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        if(configSP == null){
            return defaultValue;
        }
        return configSP.getBoolean(key,defaultValue);
    }

    /**
     * 读取配置
     * @param key
     * @param defaultValue
     * @return int
     */
    public int readConfig(String key,int defaultValue){
        if(configSP == null){
            configSP = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        if(configSP == null){
            return defaultValue;
        }
        return configSP.getInt(key,defaultValue);
    }


    public long readConfig(String key,long defaultValue){
        if(configSP == null){
            configSP = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        if(configSP == null){
            return defaultValue;
        }
        long value;
        try {
            value = configSP.getLong(key,defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
            value = defaultValue;
        }
        return value;
    }



    public void writeConfig(String key,long value){
        if(configSP == null){
            configSP = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        if(configSP == null){
            return;
        }
        SharedPreferences.Editor editor = configSP.edit();
        editor.putLong(key, value);
        editor.apply();

    }


    /**
     * 根据key 移除sp 中的value
     * @param key
     */
    public void removeValue(String key){
        if(configSP == null){
            configSP = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        if(configSP == null){
            return;
        }
        SharedPreferences.Editor editor = configSP.edit();
        editor.remove(key);
        editor.apply();
    }
}
